package com.ymatou.userservice.testcase;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.ymatou.auth.parameter.SecurityQuestion;
import com.ymatou.auth.service.LoginAuthCall;
import com.ymatou.userservice.parameter.BriefBatchUserRequestDtoBean;
import com.ymatou.userservice.service.BriefBatchUserRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.UserLogoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*批量查询用户
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BriefBatchUserRequestDto{
	private static BriefBatchUserRequestDtoBean BriefBatchUserRequestDtoBean;
	private static BriefBatchUserRequestDtoCall BriefBatchUserRequestDtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询用户");
	}

	@Before
	public void caseUp() {
		BriefBatchUserRequestDtoBean=new BriefBatchUserRequestDtoBean();
		BriefBatchUserRequestDtoCall=new BriefBatchUserRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(BriefBatchUserRequestDtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BriefBatchUserRequestDto_001() {
		Logger.start(true,"批量查询用户");
		try {
			String url="http://p3.img.ymatou.com/upload/userlogo/original/115_764448429_o.jpg";
			Map m=UserCallService.registerByMobile();
			UserCallService.addLogoToUser(Integer.valueOf(m.get("userid").toString()), url);
			Logger.debug(m);
			List<String> userIdList=new ArrayList<>();
			Logger.debug(m);
			userIdList.add(m.get("username").toString());
			BriefBatchUserRequestDtoBean.setLoginNameList(userIdList);
			
			BriefBatchUserRequestDtoCall.setData(BriefBatchUserRequestDtoBean);
			BriefBatchUserRequestDtoCall.callService();			
			Logger.verifyEquals("200", BriefBatchUserRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", BriefBatchUserRequestDtoCall.getString("Success"), "验证Success");
			JsonArray jslist = BriefBatchUserRequestDtoCall.getJsonObject().get("Result").getAsJsonArray();
			for (JsonElement jsonElement : jslist) {
				
				Map<String,String> um=(Map<String,String>)FormatUtil.GSONToObject(jsonElement.toString(), new TypeToken<Map<String,String>>(){}.getType());
				Logger.debug(um);
				Logger.verifyEquals(m.get("username"), um.get("LoginId"), "验证LoginId");
				Logger.verifyEquals(m.get("userid").toString(), um.get("UserId").toString(), "验证UserId");
				Logger.verifyEquals(m.get("email"), um.get("LoginEmail"), "验证LoginEmail");
				Logger.verifyEquals(m.get("phone"), um.get("LoginMobile"), "验证LoginMobile");
				Logger.verifyEquals("Normal", um.get("UserType"), "验证UserType");
				UserLogoWapper uw=new UserLogoWapper();
				Map logom=uw.selectLogoByUid(m.get("userid").toString());
				Logger.debug(logom);
				Logger.verifyEquals(url, logom.get("sLogoUrl").toString(), "验证sLogoUrl");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_BriefBatchUserRequestDto_002() {
		Logger.start(true,"批量查询多个用户");
		try {
			Map<Integer,Map> lm=new HashMap();
			Map<String,Map> lmname=new HashMap();
			String url="http://p3.img.ymatou.com/upload/userlogo/original/115_764448429_o.jpg";
			for(int i=0;i<3;i++){
				Map m=UserCallService.registerByMobile();
				UserCallService.addLogoToUser(Integer.valueOf(m.get("userid").toString()), url);
			lm.put((Integer) m.get("userid"), m);
			lmname.put(m.get("username").toString(), m);
			Logger.debug(m);
			}
			//故意加一个不存在的 不会查出来
			lmname.put("-1", new HashMap<>());
			List<String> usernamelist=new ArrayList<>();
			 Iterator<Entry<String, Map>> iset = lmname.entrySet().iterator();
			 while(iset.hasNext()){
				 usernamelist.add(iset.next().getKey());
			 }
			 
			BriefBatchUserRequestDtoBean.setLoginNameList(usernamelist);
			
			BriefBatchUserRequestDtoCall.setData(BriefBatchUserRequestDtoBean);
			BriefBatchUserRequestDtoCall.callService();			
			Logger.verifyEquals("200", BriefBatchUserRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", BriefBatchUserRequestDtoCall.getString("Success"), "验证Success");
			JsonArray jslist = BriefBatchUserRequestDtoCall.getJsonObject().get("Result").getAsJsonArray();
			for (JsonElement jsonElement : jslist) {
				Map<String,String> um=(Map<String,String>)FormatUtil.GSONToObject(jsonElement.toString(), new TypeToken<Map<String,String>>(){}.getType());
				Logger.debug(um);
				Map m=lm.get(Integer.valueOf(um.get("UserId")));
				Logger.verifyEquals(m.get("username"), um.get("LoginId"), "验证LoginId");
				Logger.verifyEquals(m.get("email"), um.get("LoginEmail"), "验证LoginEmail");
				Logger.verifyEquals(m.get("phone"), um.get("LoginMobile"), "验证LoginMobile");
				Logger.verifyEquals("Normal", um.get("UserType"), "验证UserType");
				UserLogoWapper uw=new UserLogoWapper();
				Map logom=uw.selectLogoByUid(m.get("userid").toString());
				Logger.debug(logom);
				Logger.verifyEquals(url, logom.get("sLogoUrl").toString(), "验证sLogoUrl");
				
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_BriefBatchUserRequestDto_003() {
		Logger.start(true,"不输入参数，失败");
		try {
			BriefBatchUserRequestDtoBean.setLoginNameList(null);
			BriefBatchUserRequestDtoCall.setData(BriefBatchUserRequestDtoBean);
			BriefBatchUserRequestDtoCall.callService();			
			Logger.verifyEquals("100", BriefBatchUserRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", BriefBatchUserRequestDtoCall.getString("Success"), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_BriefBatchUserRequestDto_004() {
		Logger.start(true,"输入参数不存在，失败");
		try {
			List<String> list=new ArrayList<>();
			list.add("-1000");
			BriefBatchUserRequestDtoBean.setLoginNameList(list);
			BriefBatchUserRequestDtoCall.setData(BriefBatchUserRequestDtoBean);
			BriefBatchUserRequestDtoCall.callService();			
			Logger.verifyEquals("200", BriefBatchUserRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("true", BriefBatchUserRequestDtoCall.getString("Success"), "验证Success");
			JsonArray jslist = BriefBatchUserRequestDtoCall.getJsonObject().get("Result").getAsJsonArray();
			Logger.verifyEquals(0, jslist.size(), "验证没有查询到数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_BriefBatchUserRequestDto_005() {
		Logger.start(true,"输入参数超限，失败");
		try {
			List<String> list=new ArrayList<>();
			for(Integer i=0;i<102;i++){
				list.add(i.toString());
			}
			BriefBatchUserRequestDtoBean.setLoginNameList(list);
			BriefBatchUserRequestDtoCall.setData(BriefBatchUserRequestDtoBean);
			BriefBatchUserRequestDtoCall.callService();			
			Logger.verifyEquals("400", BriefBatchUserRequestDtoCall.getString("ResponseCode"), "验证ResponseCode");
			Logger.verifyEquals("false", BriefBatchUserRequestDtoCall.getString("Success"), "验证Success");
			Logger.verifyIsContains("超限",BriefBatchUserRequestDtoCall.getString("LastErrorMessage"), "登录名个数超限（max: 100）");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}